﻿using Ekton_Manage_DomainCore.Aggregate;
using Ekton_Manage_DTO.RequestModels.User;
using Ekton_Manage_Framework.Methods;
using Ekton_Manage_IBusiness;
using Ekton_Manage_InfrastructureCore.Common;
using Ekton_Manage_InfrastructureCore.Repositories;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;


namespace Ekton_Manage_Business
{
    public class Company : ICompany
    {
 
        private readonly SerializeParameter _serialize;
        private readonly IBaseRepository<company, int> _baseMaterialRepository;

        public Company(SerializeParameter serialize, IBaseRepository<company, int> baseMaterialRepository)
        {
           
            _serialize = serialize;
            _baseMaterialRepository = baseMaterialRepository;
        }

        /// <summary>
        /// 删除公司组织管理
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Message> DeleteOrganizeInfo(int id)
        {
            var list = await _baseMaterialRepository.DeleteSingleData(id);
            return new Message(list);
        }

        /// <summary>
        /// 查询全部组织管理
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<Tuple<List<company>, int>> GetCompany(QueryDataParame query)
        {
            var dicData = await _serialize.ParameConvertToDict(query.parame);
            Expression<Func<company, bool>> expression = m => true;
            if (dicData.ContainsKey("company_name") && !string.IsNullOrEmpty(dicData["company_name"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.company_name.Contains(dicData["company_name"].ToString()));
            }

            if (dicData.ContainsKey("phone") && !string.IsNullOrEmpty(dicData["phone"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.phone.Contains(dicData["phone"].ToString()));
            }

            if (dicData.ContainsKey("mold") && !string.IsNullOrEmpty(dicData["mold"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.mold.Contains(dicData["mold"].ToString()));
            }

            return await _baseMaterialRepository.QueryPageData(query, expression);

        }
        /// <summary>
        /// 添加公司组织管理
        /// </summary>
        /// <param name="prouderType"></param>
        /// <returns></returns>

        public async Task<Message> PostCompany(DtoInsertCompany prouderType)
        {
            var userInfo = await _baseMaterialRepository.InsertSingleData(new company
            {
                genre = prouderType.genre,
                company_name = prouderType.company_name,
                mold = prouderType.mold,
                contach_address = prouderType.contach_address,
                phone = prouderType.phone,
                scale = prouderType.scale,
                linkman = prouderType.linkman,
                measure = prouderType.measure,
                remark = prouderType.remark
            });
            return new Message(userInfo.Item1);
        }
        /// <summary>
        /// 修改公司组织管理
        /// </summary>
        /// <param name="prouderTypeId"></param>
        /// <returns></returns>
        public async Task<Message> PutCompany(DtoInsertCompany prouderTypeId)
        {
            var companys = await _baseMaterialRepository.QuerySingleData(prouderTypeId.id);
            if (companys == null) return new Message(false, 202);
            companys.genre = prouderTypeId.genre;
            companys.company_name = prouderTypeId.company_name;
            companys.mold = prouderTypeId.mold;
            companys.contach_address = prouderTypeId.contach_address;
            companys.phone = prouderTypeId.phone;
            companys.scale = prouderTypeId.scale;
            companys.linkman = prouderTypeId.linkman;
            companys.measure = prouderTypeId.measure;
            companys.remark = prouderTypeId.remark;

            var result = await _baseMaterialRepository.UpdateSingleData(companys);
            return new Message(result.Item1);
        }
    }
}
